//
//  CategoryCell.swift
//  ListenFM
//
//  Created by szq on 2017/8/9.
//  Copyright © 2017年 胡定锋. All rights reserved.
//

import UIKit
import Kingfisher

class Dis_CategoryCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource {

    
    var imageArray = NSMutableArray()
    var titleArray = NSMutableArray()
    
    var scrollView = UIScrollView()
    var collectionView: UICollectionView!
    
    var _testStr: String?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpViews()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    var imageArray: NSMutableArray!{
//        
//        didSet{
//            
//            print(imageArray)
//        }
//    }
    
    func setUpViews(){
        
        let width = (2 * SCR_WIDTH - 4 * 10)/6 - 10
        scrollView.frame = CGRect(x:0,y:0,width:SCR_WIDTH,height:3 * (width + 10))
        scrollView.isPagingEnabled = true
        
        let layout = UICollectionViewFlowLayout.init()
        let xSpace = CGFloat(10);
        let widthtemp = ((2 * SCR_WIDTH) - 4 * xSpace)/6-10;
        layout.itemSize = CGSize.init(width: widthtemp, height: widthtemp)
        layout.sectionInset = UIEdgeInsets.init(top:5, left:5, bottom:0, right:10)
        
        collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: 2 * SCR_WIDTH, height: 3*(width + 10)), collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = UIColor.white
        collectionView.register(UINib(nibName:"Dis_CategoryCollectionCell", bundle:nil),forCellWithReuseIdentifier: "Dis_CategoryCollectionCell")
        scrollView.contentSize = CGSize.init(width: 2 * SCR_WIDTH, height: 0)
        contentView.addSubview(scrollView)
        scrollView.addSubview(collectionView)
        
        
        for i in 1..<10 {
            let str = String(format:"emotion%d",i)
            imageArray.add(str)
        }
        for i in 1...9 {
            let str = String(format:"scene%d",i)
            imageArray.add(str)
        }
        titleArray = ["烦躁","悲伤","孤独","弃疗","减压","无奈","快乐","感动","迷茫","睡前","旅行","散步","坐车","独处","失恋","失眠","随便","无聊"]
        
        collectionView.reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageArray.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell : UICollectionViewCell?
        
        cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "Dis_CategoryCollectionCell", for: indexPath) as! Dis_CategoryCollectionCell
        
        let icon = cell?.viewWithTag(101) as! UIImageView
        let title = cell?.viewWithTag(102) as! UILabel
        icon.image = UIImage(named:imageArray[indexPath.row] as! String)
        title.text = (titleArray[indexPath.row] as! String)

        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        print(indexPath.row)
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    
}
